package multithreading.g1;


/**
 * @author www.javavideokurs.de
 */
public class SynchedThreads1 implements Runnable {

	volatile boolean alive;
	volatile long i;

	@Override
	public void run() {
		System.out.println("Thread starts...");
		i = 0;
		alive = true;
		
		while (true) {
			if (alive) {
				i++;
			} else {
				break;
			}
		}

		System.out.println("Thread ends...");
	}

	public static void main(String[] args) {
		SynchedThreads1 c = new SynchedThreads1();
		new Thread(c).start();

		while (Thread.activeCount() > 1) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			long iNow = c.i;
			System.out.println(iNow);
			if (iNow >= LIMIT) {
				c.alive = false;
				System.out.println("THREAD STOPPED! (alive = " + c.alive + ")");

			}
		}
		System.out.println("Program exits...");
	}

	private static final long LIMIT = 100000000L;
}
